import codecs
import collections
import json
from oslo_serialization import jsonutils
import logging
import sys
import unittest

LOG = logging.getLogger(__name__)
CONF_PATH = 'firstprojectonmoshub\\neutron_shared_networks.json'

# $env:PYTHONPATH = "g:/Program Data/Document/Visual Studio 2019/Projects/Python/"; python .\firstprojectonmoshub\policy_test.py

class Enforcer(object):
    def enforce(self, rule, target, creds, do_raise=False,
                exc=None, *args, **kwargs):

        if isinstance(creds, str):
            creds = self._map_context_attributes_into_creds(creds)
        elif not isinstance(creds, collections.MutableMapping):
            msg = (
                'Expected type oslo_context.context.RequestContext, dict or '
                'the output of '
                'oslo_context.context.RequestContext.to_policy_values but '
                'got %(creds_type)s instead' % {'creds_type': type(creds)}
            )
            LOG.warning(msg)
            sys.exit(1)


class Testclass(unittest.TestCase):
    def test_enforcer(self):
        class InvalidContext(object):
            pass
        self.enforcer = Enforcer()
        request_context = InvalidContext()
        target_dict = {}
        self.assertRaises(
            SystemExit,
            self.enforcer.enforce,
            rule='fake_rule',
            target=target_dict,
            creds=request_context
        )


def json_test(path=CONF_PATH):
    with open(path, "rb") as json_fl:
        CONF_CACHE = jsonutils.load(json_fl)
        # CONF_CACHE = load(json_fl)
    print(f"{CONF_CACHE = }")


def load(fp, encoding='utf-8', **kwargs):
    """Deserialize ``fp`` to a Python object.

    :param fp: a ``.read()`` -supporting file-like object
    :param encoding: encoding used to interpret the string
    :param kwargs: extra named parameters, please see documentation \
    of `json.loads <https://docs.python.org/2/library/json.html#basic-usage>`_
    :returns: python object
    """
    return json.load(codecs.getreader(encoding)(fp), **kwargs)


if __name__ == '__main__':
    json_test()
    # unittest.main()
